package com.qiall.ali;

import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author
 */
public class DingTalk {


    public static String ERROR_MESSAGE_SEND = "https://oapi.dingtalk.com/robot/send?access_token=d9ef015356c4f66a6325020b1d0925f8600525542a2b0db7ab35bdafeb216067";

    private static RequestConfig commonRequestConfig = RequestConfig.custom()
            //建立连接后，发出请求和收到响应的最大超时时间
            .setSocketTimeout(5000)
            //连接服务端的超时时间
            .setConnectionRequestTimeout(5000)
            //建立连接的超时时间
            .setConnectTimeout(5000)
            .build();

    /**
     * 发送post请求
     *
     * @param url
     * @param textMsg
     * @return
     */
    private static String sendPostRequest(String url, String textMsg) {

        String result = null;

        DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
        CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(commonRequestConfig)
                .setRetryHandler(retryHandler).build();

        HttpPost httppost = new HttpPost(url);
        httppost.addHeader("Content-Type", "application/json; charset=utf-8");
        StringEntity se = new StringEntity(textMsg, "utf-8");
        httppost.setEntity(se);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httppost);
        } catch (IOException e) {
            e.printStackTrace();
        }

        int statusCode = response.getStatusLine().getStatusCode();
        if (HttpStatus.SC_OK == statusCode) {
            HttpEntity entity = response.getEntity();
            if (null != entity) {
                try {
                    result = EntityUtils.toString(entity, "UTF-8");
                    System.out.println(result);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static void sendMessage(String url, String content, String[] atMobiles) {
        try {
            TextMsg textMsg = new TextMsg();
            textMsg.setMsgtype("text");
            Text text = new Text();
            text.setContent(content);
            textMsg.setText(text);

            At at = new At();
            at.setAtAll(false);
            if (atMobiles != null && atMobiles.length > 0) {
                at.setAtMobiles(atMobiles);
            }
            textMsg.setAt(at);
            Gson gs = new Gson();
            String cont = gs.toJson(textMsg);
            sendPostRequest(url, cont);

        } catch (Exception e) {

        }
    }

    private static class TextMsg {
        private String msgtype;

        private Text text;

        private At at;

        public TextMsg() {
        }

        public String getMsgtype() {
            return msgtype;
        }

        public void setMsgtype(String msgtype) {
            this.msgtype = msgtype;
        }

        public Text getText() {
            return text;
        }

        public void setText(Text text) {
            this.text = text;
        }

        public At getAt() {
            return at;
        }

        public void setAt(At at) {
            this.at = at;
        }
    }

    private static class Text {
        private String content;

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }

    private static class At {

        private String[] atMobiles;
        private Boolean isAtAll = false;


        public void setAtMobiles(String[] atMobiles) {
            this.atMobiles = atMobiles;
        }


        public void setAtAll(Boolean atAll) {
            isAtAll = atAll;
        }
    }

    public static void main(String[] args) throws Exception {
        sendMessage(ERROR_MESSAGE_SEND, "【日志记录】:发送测试日志", null);
    }
}
